﻿using Microsoft.AspNetCore.Mvc;
using StarsAdmin.Application.Api.Dtos;
using StarsAdmin.Application.Api.Services;
using StarsAdmin.Application.Menu.Dtos;
using StarsAdmin.Core.Attributes;
using StarsAdmin.Core.Models;
using StarsAdmin.Web.Core.Conttrollers;
using System.ComponentModel.DataAnnotations;

namespace StarsAdmin.WebApi.Conttrollers
{
    /// <summary>
    /// 接口管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Order(5)]
    public class ApiController : BaseController
    {
        private readonly IApiService _apiService;

        public ApiController(IApiService apiService)
        {
            _apiService = apiService;
        }

        /// <summary>
        /// 获取所有
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResultModel<List<ApiDto>>> GetAllAsync([FromQuery] BaseQueryInput input)
        {
            return (await _apiService.GetAllAsync(input)).ResultFormat();
        }

        /// <summary>
        /// 分页获取
        /// </summary>
        /// <param name="input">分页查询输入</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResultModel<Page<ApiDto>>> GetPageAsync([FromQuery] PageQueryInput input)
        {
            return (await _apiService.GetPageAsync(input)).ResultFormat();
        }

        /// <summary>
        /// 根据Id获取数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResultModel<ApiDto>> GetByIdAsync([Required] long id)
        {
            return (await _apiService.GetByIdAsync(id)).ResultFormat("没有找到该条数据", 404);
        }

        /// <summary>
        /// 根据Id集合获取数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResultModel<List<ApiDto>>> GetByIdsAsync([FromQuery] BaseIdsInput input)
        {
            return (await _apiService.GetByIdsAsync(input.Ids)).ResultFormat();
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ResultModel<bool>> UpdateAsync(UpdateApiInput input)
        {
            return (await _apiService.UpdateAsync(input)).ResultFormat("更新失败");
        }

        /// <summary>
        /// 启用
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ResultModel<bool>> EnabledAsync([Required] long id)
        {
            return (await _apiService.EnabledAsync(id)).ResultFormat("切换状态失败");
        }

        /// <summary>
        /// 同步
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultModel<bool>> SyncApiAsync()
        {
            return (await _apiService.SyncApiAsync()).ResultFormat("同步Api接口失败");
        }
    }
}